﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Collections;
using GPCSC.Web;
using NickLee.Web.UI;
using NickLee.Common;
using System.Text;
using GPCSC.Web.sjsbyes;

namespace GPCSC.Web.sjs
{
    public partial class adminlist : PageBase
    {
        public double[] ad = new double[26];
        
        public string add = "0";


        public double[] su = new double[26];
      
        public string asum = "0";
        public string cout = "";

        

        protected void Page_Load(object sender, EventArgs e)
        {
            if(! IsPostBack)
            {
               buildpost("");
            Label1.Text = DateTime.Now.Month.ToString();
            nmonth.Value  = DateTime.Now.Month.ToString();
            }
            
        }
        protected void buildpost(string type)
        {
            Hashtable htable = new Hashtable();
            DataSet ds = new DataSet();
            //读取部门列表
            if (type == "")
            {
                ds = new SqlReadXml().ReaderDataSet("sel_POST_all", htable);
            }
            else if (type == "1")
            {
                htable.Add("@tt",0);
                ds = new SqlReadXml().ReaderDataSet("sel_POST_type", htable);
            }
            else if (type == "2")
            {
                htable.Add("@tt", 1);
                ds = new SqlReadXml().ReaderDataSet("sel_POST_type", htable);
            }
            sumss(ds, "newSelAllbypostid_km", DateTime.Now.Month.ToString(), DateTime.Now.Month.ToString(),DateTime.Now.Year);
        }
        #region
        protected void sumss(DataSet dsname, string sql, string kmonth, string jmonth,int year)
        {
            
            DataTable DT = new DataTable();
            DT.Columns.Add("PostName");
          
            DT.Columns.Add("zday");
            DT.Columns.Add("XJCDYTS");
            DT.Columns.Add("ZXMQRJPS");
            DT.Columns.Add("ZCRHTS");
            DT.Columns.Add("ZDZHTS");
            DT.Columns.Add("GKJFTS");
            DT.Columns.Add("JDXFAJ");
            DT.Columns.Add("yjaxf");
            DT.Columns.Add("ZDXFBAJS");
            DT.Columns.Add("ZDXFBAJWCS");
            DT.Columns.Add("KNQZZHS");
            DT.Columns.Add("WWKNQZQK_YZFHS");
            DT.Columns.Add("YBFHS");
            DT.Columns.Add("WWJZZHLJJE");
            DT.Columns.Add("ZKZTHCS");
            DT.Columns.Add("Z_CJRC");
            DT.Columns.Add("XSZCXJCS");
            DT.Columns.Add("X_CJRC");
            DT.Columns.Add("CJLDTS");
            DT.Columns.Add("WJCDWFCZJLJJE");
            DT.Columns.Add("wts");
            DT.Columns.Add("jjs");
            DT.Columns.Add("sss");
            DT.Columns.Add("kns");
            DT.Columns.Add("ggs");
            DT.Columns.Add("dybg");

            

            if (dsname != null)
            {
                if (dsname.Tables.Count > 0)
                {
                    if (dsname.Tables[0].Rows.Count > 0)
                    {
                        cout = dsname.Tables[0].Rows.Count.ToString();
                        //循环遍历每个部门
                        foreach (DataRow dr in dsname.Tables[0].Rows)
                        {
                            
                            string postid = dr["CL_POST_SOID"].ToString();
                            Hashtable htables = new Hashtable();
                            htables.Add("@postid", postid);
                            htables.Add("@year", year);
                            htables.Add("@kmonth", kmonth);
                            htables.Add("@jmonth", jmonth);
                            DataSet ds = new SqlReadXml().ReaderDataSet(sql, htables);
                           //为每个部门 创建dataRow
                            
                            DataRow drr = DT.NewRow();
                            drr["PostName"] = "<a href='#' onclick='openNs(\"" + postid + "\",\"" + kmonth + "\",\"" + jmonth + "\",\"" + year + "\")'>" + dr["PostName"].ToString() + "</a>";

                            if (ds != null)
                            {
                                if (ds.Tables.Count > 0)
                                {
                                    if (ds.Tables[0].Rows.Count > 0)
                                    {
                                        for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                                        {
                                            ad[0] += Usual.f_convertDobule(ds.Tables[0].Rows[i]["zday"].ToString());//下基层活动天数
                                            ad[1] += Usual.f_convertDobule(ds.Tables[0].Rows[i]["XJCDYTS"].ToString());//下基层调研
                                            ad[2] += Usual.f_convertDobule(ds.Tables[0].Rows[i]["ZXMQRJPS"].ToString());//民情
                                            ad[3] += Usual.f_convertDobule(ds.Tables[0].Rows[i]["ZCRHTS"].ToString());//走村入户
                                            ad[4] += Usual.f_convertDobule(ds.Tables[0].Rows[i]["ZDZHTS"].ToString());//驻点住户
                                            ad[5] += Usual.f_convertDobule(ds.Tables[0].Rows[i]["GKJFTS"].ToString());//公开接访天数
                                            ad[6] += Usual.f_convertDobule(ds.Tables[0].Rows[i]["JDXFAJ"].ToString());//接待信访件数
                                            ad[7] += Usual.f_convertDobule(ds.Tables[0].Rows[i]["yjaxf"].ToString());//已结案
                                            ad[8] += Usual.f_convertDobule(ds.Tables[0].Rows[i]["ZDXFBAJS"].ToString());//重点信访
                                            ad[9] += Usual.f_convertDobule(ds.Tables[0].Rows[i]["ZDXFBAJWCS"].ToString());//重点信访包案。。
                                            ad[10] += Usual.f_convertDobule(ds.Tables[0].Rows[i]["KNQZZHS"].ToString());//已走访困难
                                            ad[11] += Usual.f_convertDobule(ds.Tables[0].Rows[i]["WWKNQZQK_YZFHS"].ToString());//已走访挂钩。。
                                            ad[12] += Usual.f_convertDobule(ds.Tables[0].Rows[i]["YBFHS"].ToString());// 已帮扶挂钩。。
                                            ad[13] += Usual.f_convertDobule(ds.Tables[0].Rows[i]["WWJZZHLJJE"].ToString());//慰问捐助金额。。
                                            ad[14] += Usual.f_convertDobule(ds.Tables[0].Rows[i]["ZKZTHCS"].ToString());
                                            ad[15] += Usual.f_convertDobule(ds.Tables[0].Rows[i]["Z_CJRC"].ToString());
                                            ad[16] += Usual.f_convertDobule(ds.Tables[0].Rows[i]["XSZCXJCS"].ToString());
                                            ad[17] += Usual.f_convertDobule(ds.Tables[0].Rows[i]["X_CJRC"].ToString());
                                            ad[18] += Usual.f_convertDobule(ds.Tables[0].Rows[i]["CJLDTS"].ToString());
                                            ad[19] += Usual.f_convertDobule(ds.Tables[0].Rows[i]["WJCDWFCZJLJJE"].ToString());
                                            ad[25] += Usual.f_convertDobule(ds.Tables[0].Rows[i]["dybg"].ToString());
                                        }
                                       
                                    }
                                    
                                }
                            }
                            drr["zday"] = ad[0];
                            drr["XJCDYTS"] = ad[1];
                            drr["ZXMQRJPS"] = ad[2];
                            drr["ZCRHTS"] = ad[3];
                            drr["ZDZHTS"] = ad[4];
                            drr["GKJFTS"] = ad[5];
                            drr["JDXFAJ"] = ad[6];
                            drr["yjaxf"] = ad[7];
                            drr["ZDXFBAJS"] = ad[8];
                            drr["ZDXFBAJWCS"] = ad[9];
                            drr["KNQZZHS"] = ad[10];
                            drr["WWKNQZQK_YZFHS"] = ad[11];
                            drr["YBFHS"] = ad[12];
                            drr["WWJZZHLJJE"] = ad[13];

                            drr["ZKZTHCS"] = ad[14];
                            drr["Z_CJRC"] = ad[15];
                            drr["XSZCXJCS"] = ad[16];
                            drr["X_CJRC"] = ad[17];
                            drr["CJLDTS"] = ad[18];
                            drr["WJCDWFCZJLJJE"] = ad[19];
                            drr["dybg"] = ad[25];

                            adwt(dr["CL_POST_SOID"].ToString(), kmonth, jmonth, drr,year);//问题总计
                            jjwt(dr["CL_POST_SOID"].ToString(), kmonth, jmonth, drr,year);//解决问题统计
                            Bss(dr["CL_POST_SOID"].ToString(), kmonth, jmonth, drr,year);//办实事
                            knAgg(dr["CL_POST_SOID"].ToString(), drr,year);
                            DT.Rows.Add(drr);
                            for (int i = 0; i < 20; i++)
                            {
                                su[i] += ad[i];
                                ad[i] = 0;
                            }
                            su[25] += ad[25];
                            ad[25] = 0;

                        }
                    }
                }
            }
            Repeater1.DataSource = DT;
            Repeater1.DataBind();
        }

        // 问题统计
        protected void adwt(string userid, string kmonth, string jmonth,DataRow drr,int year)
        {
            int wts = 0;
            Hashtable htable = new Hashtable();
            htable.Add("@lgid", userid);
            htable.Add("@year", year);
            htable.Add("@kmonth", kmonth);
            htable.Add("@jmonth", jmonth);
            DataSet dwts = new SqlReadXml().ReaderDataSet("countWT_post", htable);
            if (dwts.Tables[0].Rows.Count > 0)
            {
                wts = Usual.f_convertInt32(dwts.Tables[0].Rows[0]["wts"].ToString());
            }
            //adds[20].Append("<td  class=\"td\" >" + wts.ToString() + "</td>");
            drr["wts"] = wts;
            su[20] += wts;
        }

        //解决问题统计
        protected void jjwt(string userid, string kmonth, string jmonth, DataRow drr,int year)
        {
            int wts = 0;
            Hashtable htable = new Hashtable();
            htable.Add("@lgid", userid);
            htable.Add("@year", year);
            htable.Add("@kmonth", kmonth);
            htable.Add("@jmonth", jmonth);
            DataSet dwts = new SqlReadXml().ReaderDataSet("countjjWT_post", htable);
            if (dwts.Tables[0].Rows.Count > 0)
            {
                wts = Usual.f_convertInt32(dwts.Tables[0].Rows[0]["jwts"].ToString());
            }
            drr["jjs"] = wts;
            su[21] += wts;
        }
        //办实事情况
        protected void Bss(string userid, string kmonth, string jmonth, DataRow drr,int year)
        {
            int wts = 0;
            Hashtable htable = new Hashtable();
            htable.Add("@lgid", userid);
            htable.Add("@year", year);
            htable.Add("@kmonth", kmonth);
            htable.Add("@jmonth", jmonth);
            DataSet dwts = new SqlReadXml().ReaderDataSet("counBss_post", htable);
            if (dwts.Tables[0].Rows.Count > 0)
            {
                wts = Usual.f_convertInt32(dwts.Tables[0].Rows[0]["jwts"].ToString());
            }
            drr["sss"] = wts;
            su[22] += wts;
        }

        //困难及挂钩结对户数
        protected void knAgg(string postid, DataRow drr,int year)
        {
            int kns = 0;
            int ggs = 0;
          
            DataSet dsname = new Summ().PostUser(postid);
            if (dsname.Tables.Count > 0)
            {
                if (dsname.Tables[0].Rows.Count > 0)
                {
                    foreach (DataRow dr in dsname.Tables[0].Rows)
                    {
                        Hashtable htable = new Hashtable();
                        htable.Add("@lgid", dr["CL_USER_SOID"].ToString());
                        DataSet dwts = new SqlReadXml().ReaderDataSet("selks", htable);
                        if (dwts.Tables[0].Rows.Count > 0)
                        {

                            foreach (DataRow drs in dwts.Tables[0].Rows)
                            {
                                kns += Usual.f_convertInt32(drs["knqzzhs"].ToString());
                                ggs += Usual.f_convertInt32(drs["ggjdhs"].ToString());

                            }
                        }
                    }
                }
            }
            su[23] += kns;
            su[24] += ggs;
            drr["kns"] = kns;
            drr["ggs"] = ggs;
        }
        #endregion

        protected void LCallBack_Callback(object sender, NickLee.Web.UI.CallBackEventArgs e)
        {
            try
            {
               string type = e.Parameters[0].ToString();
               if (type == "0")
               {
                 //  dsf();
                   dsf(DateTime.Now.Month.ToString(), DateTime.Now.Month.ToString(),e.Parameters[1].ToString(),DateTime.Now.Year);
               }
               else if (type == "1")//季度
               {
                  
                   int jd = Usual.f_convertInt32(e.Parameters[1].ToString());
                   int kmonth = 0;
                   int jmonth = 0;
                   switch (jd)
                   {
                       case 1:
                           kmonth = 1;
                           jmonth = 3;
                           break;
                       case 2:
                           kmonth = 4;
                           jmonth = 6;
                           break;
                       case 3:
                           kmonth = 7;
                           jmonth = 9;
                           break;
                       case 4:
                           kmonth = 10;
                           jmonth = 12;
                           break;
                       default:
                           kmonth = 1;
                           jmonth = 12;
                           break;

                   }
                   int year = Usual.f_convertInt32(e.Parameters[3].ToString());
                   dsf(kmonth.ToString(), jmonth.ToString(), e.Parameters[2].ToString(), year);

               }
               else if(type=="2")//月份
               {
                   int kmonth = Usual.f_convertInt32(e.Parameters[1].ToString());
                   int jmonth = Usual.f_convertInt32(e.Parameters[2].ToString());
                   int count = e.Parameters.Length;
                   int year = 0;
                   if (count > 4)
                   {
                       year = Usual.f_convertInt32(e.Parameters[4].ToString());
                   }
                   else
                   {
                       year = DateTime.Now.Year;
                   }
                   dsf(kmonth.ToString(), jmonth.ToString(),e.Parameters[3].ToString(),year);
               }
            }
            catch (Exception ee)
            {
                throw ee;
            }
            finally
            {
                LPanel.RenderControl(e.Output);
            }
        }

        protected void Button1_Click1(object sender, EventArgs e)
        {

            dsf(DateTime.Now.Month.ToString(), DateTime.Now.Month.ToString(),"",DateTime.Now.Year);
        }
        protected void dsf(string kmonth,string jmonth,string posttype,int year)
        {
            for (int i = 0; i < 26; i++)
            {
                su[i] = 0;
            }
           // string postid = this.Text1.Value;
            string likes = this.dw.Value.Trim();
            if (likes != "")
            {
                likes = "%" + likes + "%";
                Hashtable htab = new Hashtable();
                htab.Add("@like", likes);
                DataSet ds = new DataSet();
                ds = new SqlReadXml().ReaderDataSet("sel_POST_bylike", htab);
                sumss(ds, "newSelAllbypostid_km", kmonth, jmonth, year);
            }
            else
            {
                Hashtable htable = new Hashtable();
                DataSet ds = new DataSet();
                //读取部门列表
                if (posttype == "")
                {
                    ds = new SqlReadXml().ReaderDataSet("sel_POST_all", htable);
                }
                else if (posttype == "1")
                {
                    htable.Add("@tt", 0);
                    ds = new SqlReadXml().ReaderDataSet("sel_POST_type", htable);
                }
                else if (posttype == "2")
                {
                    htable.Add("@tt", 1);
                    ds = new SqlReadXml().ReaderDataSet("sel_POST_type", htable);
                }
                sumss(ds, "newSelAllbypostid_km", kmonth, jmonth,year);
            }
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            byte[] userInfo = (byte[])base.o_baseTable[c_obj.s_userInfo];
            Hashtable userInfoTable = CompressData.UnCompressHashtable(userInfo);
            Model.CL_USER_S users = (Model.CL_USER_S)userInfoTable["SYS_USER"];
            string filename = "淮安市" + DateTime.Now.Year.ToString() + "年"  + "三进三帮统计表.xls";
            Response.Clear();
            Response.BufferOutput = true;
            //设定输出的字符集
            Response.Charset = "GB2312";
            //假定导出的文件名为FileName.doc
            Response.AppendHeader("Content-Disposition", "attachment;filename=" + Server.UrlEncode(filename));
            Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
            /**/
            ////设置导出文件的格式
            Response.ContentType = "application/ms-excel";
            //关闭ViewState


            // GrwResult.EnableViewState = false;
            LPanel.EnableViewState = false;


            System.Globalization.CultureInfo cultureInfo = new System.Globalization.CultureInfo("ZH-CN", true);
            System.IO.StringWriter stringWriter = new System.IO.StringWriter(cultureInfo);
            System.Web.UI.HtmlTextWriter textWriter = new System.Web.UI.HtmlTextWriter(stringWriter);

            LPanel.RenderControl(textWriter);
            //   GrwResult.RenderControl(textWriter);

            //SQLServerDAL.databyhtable dtable = new SQLServerDAL.databyhtable();
            //Hashtable htable = new Hashtable();
            //htable.Add("fileUrl", stringWriter.ToString());

            //#region  首先验证该月是否有 记录

            //string year = DateTime.Now.Year.ToString();
            //string month = DateTime.Now.Month.ToString();
            //Hashtable htab = new Hashtable();
            //htab.Add("@year", year);
            //htab.Add("@month", month);
            ////htab.Add("@POstID", users.CL_POSTID_PK.ToString());
            //DataSet ds = new SqlReadXml().ReaderDataSet("tifileds", htab);
            //if (ds.Tables[0].Rows.Count > 0)
            //{
            //    dtable.databyhtables(htable, "update", "TJfile", "FileID", ds.Tables[0].Rows[0]["FileID"].ToString());
            //}
            //else
            //{
            //    htable.Add("filename", filename);
            //    htable.Add("TJdate", DateTime.Now);
            //    htable.Add("POstID", users.CL_POSTID_PK.ToString());
            //    htable.Add("PostName", users.POSTName.ToString());
            //    htable.Add("year", DateTime.Now.Year.ToString());
            //    htable.Add("month", DateTime.Now.Month.ToString());
            //    htable.Add("cj",1);
            //    dtable.databyhtables(htable, "add", "TJfile", "", "");
            //}
            //#endregion

            // //把HTML写回浏览器
            Response.Write(stringWriter.ToString());
            Response.End();


        }
    }
}